package Econf_Audio;

import java.io.IOException;
import java.net.MalformedURLException;
 
import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.Processor;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;

import jmapps.util.StateHelper;

 
public class Receveur_Serveur_InFile extends Thread{
  
		private static String PathRecord = null;
		/**
           * @param args
           */
	// Attributs
    private static int portR;
	private static String loClient;
	public static Player player;
	private Processor processor;
	private StateHelper stateHelper;
	private String fileName;
	private DataSink fileWriter;
	
	// Constructeur
	public Receveur_Serveur_InFile(int portR, String fileName) {
		this.portR = portR;
		loClient = "127.0.0.1";
		this.fileName = fileName;
		PathRecord = "/home/jannoned/Dropbox/EConf_Record/";
//		PathRecord = "/home/jannoned/Documents/Dropbox/EConf_Record/";
	}
	

	// Methode init
          public void run() {
                  String url= "rtp://"+loClient+":"+portR+"/audio/1";
                   
                  MediaLocator mediaLocator= new MediaLocator(url);
                  
                  if (mediaLocator == null) {
//                          System.err.println("Can't build MRL for RTP");
                          System.exit(-1);
                  }
                  
                  try {
                      // crÃ©ation d'un processeur
                      processor = Manager.createProcessor(mediaLocator);
                                 
                      // crÃ©ation
                      stateHelper = new StateHelper(processor);
                      // configuration du processeur
                      stateHelper.configure();
                      // configuration de la destination => format Wave
                      processor.setContentDescriptor(new FileTypeDescriptor(FileTypeDescriptor.WAVE));
                       
                      // initialisations
                      stateHelper.realize();
                      // rÃ©cupÃ©ration de la source de sortie
                      DataSource source = processor.getDataOutput();
                      // crÃ©ation d'un MediaLocator liÃ© Ã  un fichier (destination)
                      MediaLocator destination = new MediaLocator("file://"+PathRecord+fileName+".wav");
                      // crÃ©ation d'un DataSink pour Ã©crire dans le fichier le flux entrant
                      // (synchronisation entre la source et la destination)
                      fileWriter = Manager.createDataSink(source, destination);
                      // ouveture de la lecture / enregistrement (prÃ©paration)
                      fileWriter.open();
                      // dÃ©marrage du processor et du filewriter
                      fileWriter.start();
                      // capture 2 Heures soit 7200000 ms
                    	  stateHelper.playToEndOfMedia(7200000);
                    } catch (Exception e) {
//                      System.out.println("error : " + e);
                    }
          }
          
          
         // Close la capture
          public void closeRecord(){
        	  // fermeture de la capture
              stateHelper.close();
              // fermeture du fichier
              fileWriter.close();
          }
      
}
